//
//  VRChooseMonthView.m
//  KeepAccount
//
//  Created by Veer on 2017/12/20.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "VRChooseMonthView.h"


@interface VRChooseMonthView() <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, weak)  UIPickerView *monthPicker;

@end

@implementation VRChooseMonthView

- (instancetype)init
{
    if (self = [super init]) {
        
        UIPickerView *pickerView = [[UIPickerView alloc] init];
        pickerView.showsSelectionIndicator = YES;
        pickerView.delegate = self;
        pickerView.dataSource = self;
        [self.alertView addSubview:pickerView];
        self.monthPicker = pickerView;
        [pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(40);
            make.bottom.mas_equalTo(self.alertView);
            make.left.mas_equalTo(40);
            make.right.mas_equalTo(-40);
        }];
        
        [self.monthPicker reloadAllComponents];
//        pickerView selectRow:<#(NSInteger)#> inComponent:<#(NSInteger)#> animated:<#(BOOL)#>
        
        NSDate *currentDate = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
        NSInteger thisYear = components.year;
        NSInteger thisMonth = [components month];
        [pickerView selectRow:thisYear - 1900 inComponent:0 animated:YES];
        
        
//        WeakSelf(weakSelf)
//        self.ActionBlock = ^(NSInteger index) {
//
//
//        };
    }
    return self;
}


#pragma mark - action
- (void)buttonEvent:(UIButton *)sender
{
    [super buttonEvent:sender];
    
    if (sender.tag == 100) {
        [self dissmiss];
    }else{
        NSInteger component0 = [self.monthPicker selectedRowInComponent:0];
        NSInteger component1 = [self.monthPicker selectedRowInComponent:1];
        if (self.sureBlock) {
            
            self.sureBlock(1900 + component0, component1 + 1);
        }
    }
}


#pragma mark pickerView代理
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    return 2;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 100;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (0 == component) {
        return 200;
    }else{
        
        return 12;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
//    if (pickerView.tag == YAYIHospitalSelectorTag) {
//        return;
//    }
//
//    if (0 == component) {
//        [pickerView selectRow:0 inComponent:1 animated:YES];
//        [pickerView reloadComponent:1];
//
//
//    }else if (1 == component){
//
//
//    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (0 == component) {
        
        return [NSString stringWithFormat:@"%ld年", 1900+row];
    }else if(1 == component){
        
        return [NSString stringWithFormat:@"%ld月", 1+row];;
    }
    
    
    return @"test";
}


#pragma mark - setter && getter
- (void)setYear:(NSInteger)year
{
    _year = year;
    
    [self.monthPicker selectRow:year - 2010 inComponent:0 animated:YES];
}

- (void)setMonth:(NSInteger)month
{
    _month = month;
    
    [self.monthPicker selectRow:month - 1 inComponent:1 animated:YES];
}


@end
